package net.telesing.tcmc.back.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.telesing.tcmc.back.logic.CertificateLogic;
import net.telesing.tcmc.back.logic.SectionLogic;
import net.telesing.tcmc.back.model.CertificateModel;
import net.telesing.tcmc.back.model.SectionModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.back.modelqp.SectionQp;
import net.telesing.tcmc.common.Constants;
import net.telesing.tcmc.common.QueryReturn;
import net.telesing.tcmc.core.web.Ajax;
import net.telesing.tcmc.core.web.AjaxBean;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.lf5.util.StreamUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

/**
 * 证书管理相关Action
 *
 * Created by Zmt on 2015/11/3.
 */
@At("/cer")
@IocBean
//@Filters({@By(type = CheckSession.class, args = {Constants.SESSION_KEY})})
public class CertificateAction
{
    private static final Logger log = LogManager.getLogger(CertificateAction.class);

    @Inject
    private SectionLogic sectionLogic;

    @Inject
    private CertificateLogic certificateLogic;

    /**
     * 查询对应号段下的证书
     *
     * @param sId
     * @return
     */
    @At
    @POST
    @Ok("jsp:pages.back.cer.cer")
    public Map<String, Object> queryCertificate(@Param("sid") long sId)
    {
        log.info("certificateAction --> queryCertificate");
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("cer_list", certificateLogic.queryCertificate(sId));
        return result;
    }

    /**
     * 申请证书
     *
     * @param certificateModel
     * @param sid
     * @param httpServletRequest
     * @param op
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean apply(@Param("..") CertificateModel certificateModel, HttpServletRequest httpServletRequest, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("certificateAction --> apply");
        return certificateLogic.apply(op.getId(), certificateModel, httpServletRequest);
    }

    /**
     * 批量删除证书
     *
     * @param ids
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean delete(@Param("ids") long[] ids)
    {
        log.info("certificateAction --> delete");
        return certificateLogic.delete(ids);
    }

    /**
     * 删除证书
     *
     * @param id
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean deleteCer(@Param("id") long id)
    {
        log.info("certificateAction --> deleteCer");
        return certificateLogic.delete(id);
    }

    /**
     * 下载证书文件
     *
     * @param response
     * @param ids
     * @return
     * @throws Exception
     */
    @At
    @Ok("raw")
    public File download(HttpServletResponse response, @Param("ids") long[] ids) throws Exception
    {
        log.info("certificateAction --> download");
        return certificateLogic.download(response, ids);
    }

    /**
     * 下载证书文件
     *
     * @param response
     * @param id
     * @return
     * @throws Exception
     */
    @At
    @Ok("raw")
    public File downloadCer(HttpServletResponse response, @Param("id") long id) throws Exception
    {
        log.info("certificateAction --> downloadCer");
        return certificateLogic.download(response, id);
    }

    /**
     * 根据ID获取号段
     *
     * @param id
     * @param op
     * @return
     */
    @At
    @Ok("jsp:pages.back.cer.sec_local_modify_dialog")
    public Map<String, Object> fetchSection(@Param("id") long id, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("certificateAction --> fetchSection");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("section", sectionLogic.find(id));
        return map;
    }

    /**
     * 修改码段信息（局部）
     *
     * @param sectionModel
     * @param sid
     * @param user
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean modifyLocalSection(@Param("..") SectionModel sectionModel, @Param("sid") long sid, @Attr(Constants.SESSION_KEY) UserModel user)
    {
        log.info("certificateAction --> ModifyLocalSection");
        return certificateLogic.modifyLocalSection(sectionModel, sid, user);
    }

    @At
    @Ok("jsp:pages.back.cer.cer_apply")
    public Map<String, Object> getCerType(@Param("sName") String sName)
    {
        log.info("dataAction --> fetchData");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sName", sName);
        map.put("typeDrop", certificateLogic.getCerType());
        return map;
    }

}
